package com.ym.common.constant;


import com.ym.common.utils.StringUtil;

/**
 * 错误信息码定义
 *
 * @author: 李涛
 * @version: 2020年06月12日 13:59
 */
public interface Coder {

	/**
	 * 获取code
	 *
	 * @return
	 */
	String getCode();

	/**
	 * 获取错误描述信息
	 *
	 * @return
	 */
	String getMsg();

	/**
	 * 参数构建
	 *
	 * @param params
	 * @return
	 */
	default Coder build(String... params) {
		String code = getCode();
		String msg = StringUtil.format(getMsg(), params);
		Coder coder = new Coder() {
			@Override
			public String getCode() {
				return code;
			}

			@Override
			public String getMsg() {
				return msg;
			}
		};
		return coder;
	}

	/**
	 * 系统错误码
	 */
	enum System implements Coder {


		SUCCESS("1", "请求成功"),

		FAIL("0", "操作失败"),

		BAD_REQUEST("400", "参数校验异常: {}"),

		REPEATED_SUBMIT("401", "不允许重复提交, 请稍后再试!"),

		UNSUPPORTED_TYPE("415", " 不支持的媒体类型，媒体类型 {}"),

		SYS_UNKNOW_ERROR("500", "系统未知异常"),

		BUSINESS_ERROR("502", "业务异常"),

		CODE_REPETITION("503", "编码重复：{}"),

		SIGNATURE_ERROR("504", "签名错误"),

		SIGNATURE_TIME_OUT("505", "签名超时"),

		SIGNATURE_NO_DATA("506", "签名不完整"),

		YUNXIN_FAIL("507", "云信消息解密失败"),
		;

		/**
		 * 错误码
		 */
		private String code;

		/**
		 * 错误信息
		 */
		private String msg;

		System(String code, String msg) {
			this.code = code;
			this.msg = msg;
		}

		@Override
		public String getCode() {
			return code;
		}

		@Override
		public String getMsg() {
			return msg;
		}
	}

	/**
	 * 登录相关错误码
	 */
	enum Login implements Coder {
		NO_LOGIN("1000000", "未登录"),
		TOKEN_ERROR("1000001", "token错误"),
		NO_PERMISSION("1000002", "用户无权限"),
		LOGIN_EXPIRED("1000003", "登陆过期"),
		ACCOUNT_DOES_NOT_EXIST("1000004", "账号不存在"),
		ACCOUNT_PASSWORD_IS_WRONG("1000005", "账号或密码错误"),
		USER_LOGGING_IN_MULTIPLE_PLACES("1000006", "用户在多地登录"),
		THE_NUMBER_OF_AUTHENTICATION_EXCEEDS_THE_LIMIT("1000007", "认证次数超过限制"),
		ACCOUNT_IS_LOCKED("1000008", "账号被锁定"),
		ACCOUNT_HAS_BEEN_DISABLED("1000009", "账户已被禁用"),
		VERIFICATION_CODE_ERROR("1000010", "验证码错误"),
		LOGIN_FAILED("1000011", "登录失败"),
		HEADER_FAILED("1000012", "请求header获取失败"),
		;

		/**
		 * 错误码
		 */
		private String code;

		/**
		 * 错误信息
		 */
		private String msg;

		Login(String code, String msg) {
			this.code = code;
			this.msg = msg;
		}

		@Override
		public String getCode() {
			return code;
		}

		@Override
		public String getMsg() {
			return msg;
		}
	}
}
